package com.cjw.service.impl.eventTracking;

import com.cjw.mapper.database.ResourceMapper;
import com.cjw.mapper.eventTracking.EventTrackingMapper;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.dto.eventTracking.EventDTO;
import com.cjw.pojo.entity.eventTracking.EventTracking;
import com.cjw.pojo.query.eventTracking.AddEventDTO;
import com.cjw.pojo.query.eventTracking.EventQuery;
import com.cjw.service.eventTracking.EventTrackingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : cjw
 * @date: : 2022/3/22 10:26
 * @description : EventTrackingServiceImpl
 */
@Service
public class EventTrackingServiceImpl implements EventTrackingService {
    @Autowired
    private EventTrackingMapper eventTrackingMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public Boolean access(Long id) {
        EventTracking eventTracking = eventTrackingMapper.selectByPrimaryKey(id);
        return eventTracking.getState() == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long id) {
        return eventTrackingMapper.deleteByPrimaryKey(id) == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean change(Long id) {
        EventTracking eventTracking = eventTrackingMapper.selectByPrimaryKey(id);
        int state;
        if (eventTracking.getState() == 1) {
            state = 0;
        } else
            state = 1;
        EventTracking eventTracking1 = new EventTracking();
        eventTracking1.setState(state);
        eventTracking1.setId(id);
        return eventTrackingMapper.updateByPrimaryKeySelective(eventTracking1) == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(AddEventDTO addEventDTO) {
        EventTracking eventTracking = new EventTracking();
        eventTracking.setCreateTime(new Date());
        eventTracking.setName(addEventDTO.getName());
        eventTracking.setResourceId(addEventDTO.getResourceId());
        eventTracking.setState(1);
        eventTracking.setUpdateTime(new Date());
        eventTracking.setType(addEventDTO.getType());
        return eventTrackingMapper.insert(eventTracking) == 1;
    }

    @Override
    public PageDTO<List<EventDTO>> list(EventQuery eventQuery) {
        PageDTO<List<EventDTO>> eventDTOPageDTO = new PageDTO<>();
        ArrayList<EventDTO> eventDTOS = new ArrayList<>();
        PageHelper.startPage(eventQuery.getPage(),eventQuery.getSize());
        Example example = new Example(EventTracking.class);
        Example.Criteria criteria = example.createCriteria();
        if (eventQuery.getSearch() != null && !eventQuery.getSearch().trim().equals(""))
            criteria.andLike("name", "%"+eventQuery.getSearch()+"%");
        example.setOrderByClause("id desc");
        PageInfo<EventTracking> eventTrackingPageInfo = new PageInfo<>(eventTrackingMapper.selectByExample(example));
        for (EventTracking eventTracking : eventTrackingPageInfo.getList()) {
            eventDTOS.add(toDTO(eventTracking));
        }
        eventDTOPageDTO.setDtos(eventDTOS);
        eventDTOPageDTO.setTotal((int) eventTrackingPageInfo.getTotal());
        return eventDTOPageDTO;
    }

    private EventDTO toDTO(EventTracking eventTracking) {
        EventDTO eventDTO = new EventDTO();
        eventDTO.setId(eventTracking.getId());
        eventDTO.setName(eventTracking.getName());
        eventDTO.setResourceName(resourceMapper.selectByPrimaryKey(eventTracking.getResourceId()).getName());
        eventDTO.setState(eventTracking.getState());
        String type;
        if (eventTracking.getState() == 1) {
            type = "添加";
        } else if (eventTracking.getState() == 2) {
            type = "删除";
        } else if (eventTracking.getState() == 3) {
            type = "修改";
        } else {
            type = "查询";
        }
        eventDTO.setType(type);
        return eventDTO;
    }
}
